//
//  ViewNaviBar.m
//  Zaker
//
//  Created by dllo on 16/3/10.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewNaviBar.h"

@interface ViewNaviBar ()

@property (nonatomic, retain) NSMutableArray *buttonArr;
@property (nonatomic, retain) UIView *underView;

@end

@implementation ViewNaviBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.arr = [NSMutableArray array];
        self.buttonArr = [NSMutableArray array];
//        self.underView = [[UIView alloc] init];
//        [self addSubview:self.underView];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    for (int i = 0; i < _arr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitleColor:[UIColor colorWithWhite:0.902 alpha:1.000] forState:UIControlStateNormal];
        button.frame = CGRectMake(i * SCREEN_WIDTH / 5, self.frame.size.height - 25, self.frame.size.width / 5, 25);
        [button setTitle:_arr[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        if (i == 0) {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            //button.titleLabel.font = [UIFont systemFontOfSize:25];
        }
        [button addTarget:self action:@selector(large:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [_buttonArr addObject:button];
    }
    self.underView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 2, self.frame.size.width / 5, 2)];
     _underView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_underView];
}

- (void)large:(UIButton *)button
{
    for (UIButton *btn in _buttonArr) {
        [btn setTitleColor:[UIColor colorWithWhite:0.902 alpha:1.000] forState:UIControlStateNormal];
        //btn.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //[button.titleLabel setFont:[UIFont systemFontOfSize:25]];
    [UIView animateWithDuration:0.5 animations:^{
        _underView.frame = CGRectMake(button.frame.origin.x, self.frame.size.height - 2, SCREEN_WIDTH / 5, 2);
    }];
    [self.delegate PassNaviBarWithX:button.frame.origin.x];
}

- (void)naviBarChangeOffsetWithH:(CGFloat)h
{
    for (UIButton *btn in _buttonArr) {
        [btn setTitleColor:[UIColor colorWithWhite:0.902 alpha:1.000] forState:UIControlStateNormal];
        //btn.titleLabel.font = [UIFont systemFontOfSize:17];
    }
    int i = h / SCREEN_WIDTH;
    UIButton *button = _buttonArr[i];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    //[button.titleLabel setFont:[UIFont systemFontOfSize:25]];
    [UIView animateWithDuration:0.5 animations:^{
        _underView.frame = CGRectMake(h / 5, self.frame.size.height - 2, SCREEN_WIDTH / 5, 2);
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
